/**
 * 询价.COM v1.0.0
 * http://xun-jia.com/
 *
 * Copyright (c) 2009 
 * 四川漾天空网络科技有限公司
 * 成都市天府软件园A3栋2楼
 *
 */
package com.xunjia.reports.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.opensymphony.xwork2.Action;
import com.xunjia.action.XunJiaActionSupport;
import com.xunjia.util.Constants;
import com.xunjia.util.XMLNodeConstants;
import com.xunjia.util.XMLUtils;

/**
 * @author Mark
 * 
 * @Email Mark_yang@xun-jia.com
 * 
 * @Date 2009-4-2 下午04:35:47
 * 
 */
public class ReportsAction extends XunJiaActionSupport {
	// 日志
	private static final Logger logger = Logger.getLogger(ReportsAction.class);
	// 序列码
	private static final long serialVersionUID = 5659764675637633480L;
	// 广告ID
	private String advertisementID;

	/**
	 * 输出数据广告报表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String execute() throws Exception {
		return Action.SUCCESS;
	}

	public String goCheckAdvertisementReports() throws Exception {
		logger.info("goCheckAdvertisementReports");
		try {
			response.setContentType(Constants.HTTP_HEAD_CONTENT_TYPE);
			request.setCharacterEncoding(Constants.CHARACTER_UTF8);
			response.setHeader(Constants.HTTP_HEAD_PRAGMA, Constants.HTTP_HEAD_NO_CACHE);
			response.setHeader(Constants.HTTP_HEAD_CACHE_CONTROL, Constants.HTTP_HEAD_NO_CACHE);
			response.setHeader(Constants.HTTP_HEAD_EXPIRES, "0");
			// XML文件读取类
			SAXReader xmlReader = new SAXReader();
			// 广告报表FLASH配置文件(模板文件)
			String filename = ServletActionContext.getServletContext().getRealPath(Constants.DEFAUTL_SETTING_FILE_PATH);
			// 从配置文件读取数据,去除需要动态改变数据的父元素及其所有子元素
			Document doc = XMLUtils.copyDestFile(filename, XMLNodeConstants.EXCLUDE_ELEMENT_NAME);
			// 从模板中读取出需要动态改变的数据项默认配置
			Element e = XMLUtils.getNeedChangeElement(xmlReader.read(new File(filename)), XMLNodeConstants.SEPARATOR + doc.getRootElement().getName() + XMLNodeConstants.SEPARATOR + XMLNodeConstants.EXCLUDE_ELEMENT_NAME + XMLNodeConstants.SEPARATOR + XMLNodeConstants.EXCLUDE_ELEMENT_ITEM_NAME);
			// TODO:测试数据
			List data = new ArrayList();
			data.add(1);
			data.add(1);
			// data.add(1);
			// data.add(1);
			// 添加广告点击率
			XMLUtils.appendChild(doc, data, XMLNodeConstants.EXCLUDE_ELEMENT_NAME, e, userID);
			// 获取二进制输出流
			OutputStream out = response.getOutputStream();
			// 写出XML文件
			out.write(doc.asXML().getBytes("UTF-8"));
			// 刷新二进制流缓存
			out.flush();
			// 关闭流
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("输出广告报表配置数据");
		return null;
	}

	/**
	 * 获取广告点击率
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAdvertismentClickData() throws Exception {
		String filename = ServletActionContext.getServletContext().getRealPath("/reports/amstock/amstock/data/data" + advertisementID + ".csv");
		File file = new File(filename);
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		try {
			while (true) {
				String line = br.readLine();
				if (line != null)
					response.getWriter().println(line);
				else {
					break;
				}
			}
		} catch (Exception e) {
		} finally {
			br.close();
		}
		return null;
	}

	/**
	 * @return the advertisementID
	 */
	public String getAdvertisementID() {
		return advertisementID;
	}

	/**
	 * @param advertisementID
	 *            the advertisementID to set
	 */
	public void setAdvertisementID(String advertisementID) {
		this.advertisementID = advertisementID;
	}

}
